package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.QuestionsDetailActivity;
import com.yaozheng.vocationaltraining.bean.HundredQuestionsEncyclopediaItemBean;
import com.yaozheng.vocationaltraining.bean.QuestionAnswerItemBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.view.ActionSheetDialog;
import com.yaozheng.vocationaltraining.view.AlertDialog;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import java.util.List;

/**
 * Created by moon on 2017/11/9.
 */

public class QuestionsAnswerAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;
    BitmapLoader userBitmapLoader;

    private List<QuestionAnswerItemBean> data = null;
    private int detailType;
    private int resolved;
    private int uid;
    private int usertype;
    private int selectedPosition = -1;

    public QuestionsAnswerAdapter(Activity context,
                                  List<QuestionAnswerItemBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.question_answer_list_item, null);
            holder = new ViewHolder();

            holder.userAvatarImage = (CircleImageView) convertView
                    .findViewById(R.id.userAvatarImage);
            holder.username_textview = (TextView) convertView.
                    findViewById(R.id.username_textview);
            holder.storename_textview = (TextView) convertView.
                    findViewById(R.id.storename_textview);
            holder.position_textview = (TextView) convertView.
                    findViewById(R.id.position_textview);
            holder.time_textview = (TextView) convertView.
                    findViewById(R.id.time_textview);

            holder.status_imageview = (ImageView) convertView.
                    findViewById(R.id.status_imageview);
            holder.have_adopt_imageview = (ImageView) convertView.
                    findViewById(R.id.have_adopt_imageview);
            holder.desc_textview = (TextView) convertView.
                    findViewById(R.id.desc_textview);

            holder.attachment_imageview = (ImageView) convertView.
                    findViewById(R.id.attachment_imageview);
            holder.bonus_imageview = (ImageView) convertView.
                    findViewById(R.id.bonus_imageview);

            holder.jifen_number_imageview = (TextView) convertView.
                    findViewById(R.id.jifen_number_imageview);
            holder.thumbsup_imageview = (ImageView) convertView.
                    findViewById(R.id.thumbsup_imageview);

            holder.thumbsup_num_textview = (TextView) convertView.
                    findViewById(R.id.thumbsup_num_textview);
            holder.jefen_tip_layot = (LinearLayout) convertView.
                    findViewById(R.id.jefen_tip_layot);

            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }


        holder.username_textview.setText(data.get(position).getCreator().getName());
        holder.storename_textview.setText(data.get(position).getCreator().getStoreName());
        if(!TextUtils.isEmpty(data.get(position).getCreator().getPosition()) && !"null".equals(data.get(position).getCreator().getPosition())){
            holder.position_textview.setText(data.get(position).getCreator().getPosition());
        }else {
            holder.position_textview.setText("");
        }

        holder.time_textview.setText(data.get(position).getCreatedTime());
        holder.desc_textview.setText(data.get(position).getBody());

        if(data.get(position).getLikeCnt() < 1){  //thumbsup_num_textview
            holder.thumbsup_num_textview.setVisibility(View.VISIBLE);
            holder.thumbsup_num_textview.setText("0");
        }else {
            holder.thumbsup_num_textview.setVisibility(View.VISIBLE);
            holder.thumbsup_num_textview.setText(data.get(position).getLikeCnt()+"");
        }


        holder.jefen_tip_layot = (LinearLayout) convertView.
                findViewById(R.id.jefen_tip_layot);
        holder.jifen_number_imageview = (TextView) convertView.
                findViewById(R.id.jifen_number_imageview);


        holder.userAvatarImage = (CircleImageView) convertView
                .findViewById(R.id.userAvatarImage);
        holder.status_imageview = (ImageView) convertView.
                findViewById(R.id.status_imageview);
        holder.have_adopt_imageview = (ImageView) convertView.
                findViewById(R.id.have_adopt_imageview);
        holder.attachment_imageview = (ImageView) convertView.
                findViewById(R.id.attachment_imageview);
        holder.bonus_imageview = (ImageView) convertView.
                findViewById(R.id.bonus_imageview);
        holder.thumbsup_imageview = (ImageView) convertView.
                findViewById(R.id.thumbsup_imageview);

        holder.add_system_reward_layout = (LinearLayout) convertView.findViewById(R.id.add_system_reward_layout);

        if(data.get(position).isLike()){//// 当前用户是否已经点赞
            holder.thumbsup_imageview.setImageResource(R.drawable.thumbsup);
            holder.thumbsup_imageview.setEnabled(false);
        }else {
            holder.thumbsup_imageview.setEnabled(true);
            holder.thumbsup_imageview.setImageResource(R.drawable.not_thumbsup);
            holder.thumbsup_imageview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    data.get(position).setLike(true);
//                    notifyDataSetChanged();
                    ((QuestionsDetailActivity)mContext).setLike(data.get(position).getId());
                }
            });
        }

        if(data.get(position).getScore() < 1){
            holder.jefen_tip_layot.setVisibility(View.GONE);
        }else {
            holder.jefen_tip_layot.setVisibility(View.VISIBLE);
            holder.jifen_number_imageview.setText(data.get(position).getScore()+"");
        }


        holder.have_adopt_imageview.setVisibility(View.GONE);
        holder.bonus_imageview.setVisibility(View.GONE);

        if(detailType == 1){ //如果是管理员从待审核列表进来，则显示已审核或未审核
            holder.status_imageview.setVisibility(View.VISIBLE);
            if (data.get(position).getStatus() == 1) {
                holder.status_imageview.setImageResource(R.drawable.reviewed);
            }else {
                holder.status_imageview.setImageResource(R.drawable.not_reviewed);
            }
            holder.status_imageview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    data.get(position).setStatus(data.get(position).getStatus() == 1?2:1);
                    notifyDataSetChanged();
                    //TODO调接口
                    if (data.get(position) != null &&
                            data.get(position).getStatus() == 1){ //表示已审核
                        ((QuestionsDetailActivity)mContext).reViewAnswer(data.get(position).getId(),true);
                    }else{
                        ((QuestionsDetailActivity)mContext).reViewAnswer(data.get(position).getId(),false);
                    }
                }
            });
            if (resolved == 1){//表示已解决
                if (data.get(position).getSelected() == 1){
                    holder.bonus_imageview.setVisibility(View.VISIBLE);
                }
            }
        }else if (detailType == 2 ){//采纳界面
            if (resolved == 1){//表示已解决
                holder.status_imageview.setVisibility(View.GONE);
                if (data.get(position).getSelected() == 1){
                    holder.have_adopt_imageview.setVisibility(View.VISIBLE);
                }
            }else {
                holder.status_imageview.setImageResource(R.drawable.adopt_icon);
                holder.status_imageview.setVisibility(View.VISIBLE);
                holder.status_imageview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //TODO调接口
                        new AlertDialog(mContext).builder().setTitle("您是否采纳该回答？")
                                .setMsg("您只能采纳一条，\n" +
                                        "采纳后，问题状态\n" +
                                        "会更改为已解决，\n" +
                                        "此操作不可撤消")
                                .setNegativeButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {

                                    }
                                })
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        resolved =1;
                                        data.get(position).setSelected(1);
                                        notifyDataSetChanged();

                                        ((QuestionsDetailActivity)mContext).answerSelect(data.get(position).getId());
                                    }
                                })
                                .show();

                    }
                });
            }
        }else {//回答界面
            holder.status_imageview.setVisibility(View.GONE);

        }

//        if(uid == data.get(position).getCreator().getUid()){
//            if (resolved == 1){//表示已解决
//                holder.status_imageview.setVisibility(View.GONE);
//                if (data.get(position).getSelected() == 1){
//                    holder.have_adopt_imageview.setVisibility(View.VISIBLE);
//                }
//            }else {
//                holder.status_imageview.setImageResource(R.drawable.adopt_icon);
//                holder.status_imageview.setVisibility(View.VISIBLE);
//                holder.status_imageview.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        //TODO调接口
//                        new AlertDialog(mContext).builder().setTitle("您是否采纳该回答？")
//                                .setMsg("您只能采纳一条，\n" +
//                                        "采纳后，问题状态\n" +
//                                        "会更改为已解决，\n" +
//                                        "此操作不可撤消")
//                                .setNegativeButton("取消", new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View view) {
//
//                                    }
//                                })
//                                .setPositiveButton("确定", new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View view) {
//                                        resolved =1;
//                                        data.get(position).setSelected(1);
//                                        notifyDataSetChanged();
//
//                                        ((QuestionsDetailActivity)mContext).answerSelect(data.get(position).getId());
//                                    }
//                                })
//                                .show();
//
//                    }
//                });
//            }
//        }

        if (data.get(position).getSelected() == 1){
            holder.have_adopt_imageview.setVisibility(View.VISIBLE);
        }

        if (usertype == 1 ){
            if (data.get(position).getSelected() == 1){
                holder.bonus_imageview.setVisibility(View.VISIBLE);
            }else {
                holder.bonus_imageview.setVisibility(View.GONE);
            }
        }else {
            holder.bonus_imageview.setVisibility(View.GONE);
        }

        holder.bonus_imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final EditText inputServer = new EditText(
                        mContext);
                inputServer.setInputType(InputType.TYPE_CLASS_NUMBER);
                inputServer.setBackground(null);
                android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(
                        mContext,android.R.style.Theme_DeviceDefault_Light_Dialog_Alert);
                builder.setTitle("奖励积分数据")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setView(inputServer).setNegativeButton("取消", null);
                builder.setPositiveButton("确认", null);
                final android.app.AlertDialog remarkDialog = builder
                        .create();
                remarkDialog.show();

                remarkDialog.getButton(
                        android.app.AlertDialog.BUTTON_POSITIVE)
                        .setOnClickListener(new View.OnClickListener() {

                            public void onClick(View view) {
                                if (TextUtils.isEmpty(inputServer.getEditableText()
                                        .toString().trim())) {
                                    Toast.makeText(mContext,"奖励的积分不能为空",Toast.LENGTH_SHORT).show();
                                    return;
                                } else {
                                    remarkDialog.dismiss();

                                    data.get(position).setScore(data.get(position).getScore() +
                                            Integer.parseInt(inputServer.getEditableText().toString()));
                                    notifyDataSetChanged();

                                    //TODO 调用追加奖励 接口
                                    ((QuestionsDetailActivity)mContext).addAnswerReward(data.get(position).getId(),
                                            inputServer.getEditableText().toString());
                                }
                            }
                        });
            }
        });



        holder.attachment_imageview.setVisibility(View.GONE);

        if(data.get(position).getImages() != null && data.get(position).getImages().size() > 0){
            holder.attachment_imageview.setVisibility(View.VISIBLE);
            userBitmapLoader.displayImage(data.get(position).getImages().get(0).getPath(),holder.attachment_imageview);
        }else {
            holder.attachment_imageview.setVisibility(View.GONE);
        }

        //加载图片
        userBitmapLoader.displayImage(data.get(position).getCreator().getAvatar(), holder.userAvatarImage);


        return convertView;
    }

    public void setData(List<QuestionAnswerItemBean> data,int detailType,int resolved,int uid,int usertype) {
        this.detailType = detailType;
        this.resolved = resolved;
        this.uid = uid;
        this.usertype = usertype;
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        CircleImageView userAvatarImage;

        TextView username_textview;
        TextView storename_textview;
        TextView position_textview;
        TextView time_textview;

        ImageView status_imageview;
        ImageView have_adopt_imageview;
        TextView desc_textview;

        ImageView attachment_imageview;

        ImageView bonus_imageview;
        TextView jifen_number_imageview;
        ImageView thumbsup_imageview;
        TextView thumbsup_num_textview;
        LinearLayout jefen_tip_layot;

        LinearLayout add_system_reward_layout;

    }
}
